﻿using System;
using System.Net;
using System.Windows;
 
 
 
using System.Windows.Input;
 
 
 
using System.Collections.Generic;

namespace G.Net.Oauth
{
    public class OAuthHttpGet : HttpGet
    {
        /// <summary>
        /// Initializes a new instance of <see cref="OAuthHttpGet"/> with the specified <paramref name="uri"/>.
        /// </summary>
        /// <param name="uri">The uri to identify a resource in the remote server.</param>
        public OAuthHttpGet(string uri)
            : base(uri)
        { }

        /// <summary>
        /// Constructs the uri by appending OAuth authorization query string and additional query parameters.
        /// </summary>
        /// <returns></returns>
        protected override string ConstructUri()
        {
            OauthConfiger.ValidateAccessToken();

            var uri = Uri;

            var oAuthQueryString = OauthSignHelper.ConstructOAuthQueryString(CollectAllParams(), Method, Uri, OauthConfiger.AccessToken.Secret);

            uri += "?" + oAuthQueryString;

            return uri;
        }

        /// <summary>
        /// Gets or sets additional parameters (in addition to the OAuth parameters) to be sent in the request.
        /// <remarks>Usually API related parameters.</remarks>
        /// </summary>
        public override ParamCollection Params
        {
            get
            {
                return base.Params;
            }
        }

        /// <summary>
        /// Combines the OAuth basic parameters and additional parameters.
        /// </summary>
        /// <returns></returns>
        private IEnumerable<ParamPair> CollectAllParams()
        {
            var parameters = OauthSignHelper.GetOAuthBasicParams();

            OAuthAccessToken accessToken = OauthConfiger.AccessToken;

            parameters.Add(Constants.OAuthToken, accessToken.Token);

            if (null != Params)
            {
                foreach (var item in Params)
                {
                    parameters.Add(item.Name, item.Value);
                }
            }

            return parameters;
        }
    }
}
